<?
include_once('../conn.php');
include_once('../include/func.php');
require("../user_check.php") ;
class cart{
	var $item;
	var $user;
	function __construct($item,$user){
		$this->item=$item;
		$this->user=$user;
		}
		function item_add(){
			global $db; global $tablepre;
			$check_exist = $db->select("SELECT uid,v FROM `".tablepre."session` where uid = '".$this->user."' and type = '1'");
			if($this->user<>$check_exist[0][0]){
				$result = $db->INSERT("INSERT INTO `".tablepre."session` (`uid`,`type`) VALUES ('".$this->user."','1')");
				header("Location: $nowpage?item=$_GET[item]");
				exit;
				}
				//添加session
				$exist_explode=explode(',',substr($check_exist[0][1],1)); //拆分数组
				foreach ($exist_explode as $value){
					if($value==$this->item){return ;}
				}
				//检查数组
					$vdate=$check_exist[0][1].','.$_GET[item]; //加入数组
					$updatedate = $db->UPDATE("UPDATE `".tablepre."session` SET `v`='$vdate' WHERE `uid`='".$this->user."' and type = '1'");  //更新数组
			//return count('购物车有'.$exist_explode.'个商品');
			//return $this->item;
			}
		function item_del(){
			global $db; global $tablepre;
			$check_exist = $db->select("SELECT uid,v FROM `".tablepre."session` where uid = '".$this->user."' and type = '1'");
				$exist_explode=explode(',',substr($check_exist[0][1],1)); //拆分数组
				foreach ($exist_explode as $value){
					if($value<>$this->item){$v .= ','.$value;}
					//$v .= ','.$value;
				}
				$updatedate = $db->UPDATE("UPDATE `".tablepre."session` SET `v`='$v' WHERE `uid`='".$this->user."'  and type = '1'");  //更新数组
				$db->DELETE("DELETE FROM `".tablepre."session` WHERE `v3`='".$this->item."' and type = '2' and `uid`='".$this->user."'");
				//return $v;
			}
		function count_time(){
			global $db; global $tablepre;
			$check_exist = $db->select("SELECT uid,v FROM `".tablepre."session` where uid = '".$this->user."'  and type = '1'");
			$exist_explode=explode(',',substr($check_exist[0][1],1)); //拆分数组
			if($exist_explode[0]==null){
				$updatedate = $db->UPDATE("UPDATE `".tablepre."session` SET `v`='' WHERE `uid`='".$this->user."'  and type = '1'");  //更新数组
				return CART_NOTE_EMPTY;
				}else{
			return @arr_show_li(2,CART_NOTE_COUNT_TEXT,count($exist_explode));
				}
			//return '购物车有'.count($exist_explode).'个商品';
			}
			
		function ch_order(){
			global $db; global $tablepre;
			$check_exist = $db->select("SELECT v,v2 FROM `".tablepre."session` where uid = '".$this->user."' and type = '2' and v = '".$this->item[ch_order]."'");
			if($check_exist[0][0]==null){
				$db->INSERT("INSERT INTO `".tablepre."session` (`uid`,`type`,`v`,`v2`,`v3`) VALUES ('".$this->user."','2','".$this->item[ch_order]."','".$this->item[quantity]."','".$this->item[uid]."')");
				}else{
					$db->UPDATE("UPDATE `".tablepre."session` SET `v2`='".$this->item[quantity]."',`v3`='".$this->item[uid]."' WHERE `uid`='".$this->user."' and `v` = '".$this->item[ch_order]."' and `type` = '2'");
					}
			

			}
	}
	if(isset($_GET[item])){
		$cart=new cart($_GET[item],$checkmember[0][2]);
		echo $cart->item_add();
		echo $cart->count_time();}
			elseif(isset($_GET[ch_order])){
				$cart=new cart($_GET,$checkmember[0][2]);
				$cart->ch_order();
				}elseif(isset($_GET[close_clear])){
					$db->DELETE("DELETE FROM `".tablepre."session` WHERE type = '2' and `uid`='".$checkmember[0][2]."'");
					}elseif(isset($_GET[reorder])){
						$db->UPDATE("UPDATE `".tablepre."session` SET `v`='' WHERE (`uid`='".$checkmember[0][2]."')");  //更新数组
						$db->DELETE("DELETE FROM `".tablepre."session` WHERE `uid`='".$checkmember[0][2]."' and type = '2'");
						//清空shopping cart
						$order_list = $db->select("SELECT sid FROM `".tablepre."order_item` where oid = '".$_GET[reorder]."' group by sid");
						for($i=0;$i<count($order_list);$i++){
							$stock .= ','.$order_list[$i][0];
							}
							$db->UPDATE("UPDATE `".tablepre."session` SET `v`='".$stock."' WHERE (`uid`='".$checkmember[0][2]."')");  //更新数组
							echo @arr_show_li(2,CART_NOTE_COUNT_TEXT,count($order_list));
							
						}

?>